_realname=nuspell
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.1.1
pkgrel=2
pkgdesc="Fast and safe spellchecking C++ library (mingw-w64)"
arch=('any')
url="https://nuspell.github.io/"
license=(LGPL3+)
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-catch"
             "${MINGW_PACKAGE_PREFIX}-ruby-ronn")
depends=("${MINGW_PACKAGE_PREFIX}-icu"
         "${MINGW_PACKAGE_PREFIX}-boost")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/nuspell/nuspell/archive/v${pkgver}.tar.gz")
sha256sums=("a0de090c3786df33392dde5ea8b0121f0fd852d68169834732a46f4d010990b1")
sha512sums=("239855051d9f49ba16913283090c4214a8f6a6cc290d359ab54014ff76fc297c131b67c6748bd1d4cdcec43c00dccc7f0c1bf8b07e06c9c648bff52ff193e096")

prepare() {
  # https://github.com/nuspell/nuspell/issues/71
  # once fixed upstream, delete this patching
  cd "${srcdir}/${_realname}-${pkgver}"
  sed -i 's/-I@Boost_INCLUDE_DIRS@//' nuspell.pc.in
}

build() {
  local build_dir="${srcdir}/build-${MINGW_CHOST}"
  [[ -d "$build_dir" ]] && rm -rf "$build_dir"
  mkdir "$build_dir" && cd "$build_dir"
  
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe ../"${_realname}-${pkgver}" \
    -G "MSYS Makefiles" \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    -DBUILD_SHARED_LIBS=ON

  make
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ctest
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="$pkgdir" install

  # Licenses
  cd "${srcdir}/${_realname}-${pkgver}"
  install -Dm644 COPYING      "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  install -Dm644 COPYING.LESSER "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.LESSER"
}
